package com.sdkx.app.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.activitys.MainActivity;
import com.sdkx.app.model.AirSpace;
import com.sdkx.app.model.AirSpaceGeneral;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class AirspaceGeneralFragment extends BaseFragment {
    RecyclerView recyclerView;
    AirspaceAdapter airspaceAdapter;
    @Nullable
    @Override
    public View onCreateView(   @Nullable LayoutInflater inflater, @Nullable ViewGroup container,
                                @Nullable  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_menu_airspace1, container, false);
        //返回上一级点击事件
        Button tv_all_air_spaces = (Button) view.findViewById(R.id.tv_all_air_spaces2);

        //返回上一级按钮
        tv_all_air_spaces.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // ((MainActivity)getActivity()).ShowMainMenu(0);
                goBack();

            }
        });
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        airspaceAdapter = new AirspaceAdapter();
        recyclerView.setAdapter(airspaceAdapter);
    }
    public class AirspaceAdapter extends RecyclerView.Adapter<AirspaceAdapter.ViewHolder>{
        List<AirSpaceGeneral> list  = new ArrayList<>();
      public AirspaceAdapter(){
           list =  LitePal.findAll(AirSpaceGeneral.class);
           if(list.size()== 0){
               AirSpaceGeneral general = new AirSpaceGeneral();
               general.setVisible(true);
               general.setName("自定义空域");
               list.add(general);
               general.save();
           }
        }
        public List<AirSpaceGeneral> getList() {
            return list;
        }
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(getLayoutInflater().inflate(R.layout.airspace_item,null));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.init(list.get(position));
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder{
            CheckBox checkBox;
            TextView textView;
            ImageView next;
            AirSpaceGeneral airSpaceGeneral;
            public ViewHolder(View itemView) {
                super(itemView);
                itemView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
                checkBox = itemView.findViewById(R.id.checkid);
                textView = itemView.findViewById(R.id.name);
                next = itemView.findViewById(R.id.next);
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        airSpaceGeneral.setVisible(isChecked);
                        airSpaceGeneral.update(airSpaceGeneral.getId());
                    }
                });
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("AIRSPACE_ITEM",airSpaceGeneral.getId());
                        startFragment(AirspaceFragment.class,bundle);
                    }
                });
            }
            public void init(AirSpaceGeneral general){
                airSpaceGeneral = general;
                textView.setText(general.getName());
            }
        }
    }

}